package com.mes.check.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mes.check.manager.iface.ICheckBillDetailManager;
import com.mes.basicdata.dao.iface.IOperationDao;
import com.mes.basicdata.dao.iface.IOperationMeasurementDao;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.OperationMeasurement;
import com.mes.check.dao.iface.ICheckBillDao;
import com.mes.check.domain.CheckBill;
import com.mes.check.domain.CheckBillDetail;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.SysUtil;
import com.mes.inventory.dao.iface.IMaterialDao;
import com.mes.inventory.domain.Material;
import com.mes.task.domain.OperationTask;
@Component
public class CheckBillDetailManager extends BasicManager<CheckBillDetail> implements ICheckBillDetailManager {

	@Autowired
	IOperationDao operationDao;
	@Autowired
	IOperationMeasurementDao operationMeasurementDao;
	@Autowired
	ICheckBillDao checkBillDao;
	@Autowired
	IMaterialDao materialDao;
	
	/**
	 * 根据工艺的检验要求定义产生检验单明细
	 * @param checkBillUid
	 * @param operationUid
	 * @throws Exception
	 */
	@Override
	public void generateCheckBillDetailsByOperationUid(String checkBillUid, String operationUid) throws Exception {
		CheckBill checkBill = checkBillDao.getById(checkBillUid);
		OperationTask operationTask = checkBill.getCheckTask().getOperationTask();
		List<Material> materials = materialDao.getMaterialsByPartUidBatchNum(operationTask.getPartUid(), operationTask.getBatchNum());
		
		Operation operation = operationDao.getById(operationUid);
		Set<OperationMeasurement> operationMeasurements = operation.getOperationMeasurements();
		
		List<CheckBillDetail> details = new ArrayList<CheckBillDetail>();
		if(materials != null && materials.size() > 0){
			for(Material material : materials){
				for(OperationMeasurement measurement : operationMeasurements){
					CheckBillDetail detail = new CheckBillDetail();
					detail.setUuid(SysUtil.getUUID());
					detail.setMaterialUid(material.getUuid());
					detail.setCheckBillUid(checkBillUid);
					detail.setSeq(measurement.getMeasurementSeq());
					detail.setMeasureBase(measurement.getMeasureBase());
					detail.setMeasurementType(measurement.getMeasurementType());
					detail.setMeasurementNumber(measurement.getMeasurementNumber());
					detail.setOperationUid(operationUid);
					detail.setStandardValue(measurement.getStandardValue());
					detail.setUbound(measurement.getUbound());
					detail.setLbound(measurement.getLbound());
					detail.setSpcStandardValue(measurement.getSpcStandardValue());
					
					details.add(detail);
				}
			}
		} else {
			for(OperationMeasurement measurement : operationMeasurements){
				CheckBillDetail detail = new CheckBillDetail();
				detail.setUuid(SysUtil.getUUID());
				detail.setCheckBillUid(checkBillUid);
				detail.setSeq(measurement.getMeasurementSeq());
				detail.setMeasureBase(measurement.getMeasureBase());
				detail.setMeasurementType(measurement.getMeasurementType());
				detail.setMeasurementNumber(measurement.getMeasurementNumber());
				detail.setOperationUid(operationUid);
				detail.setStandardValue(measurement.getStandardValue());
				detail.setUbound(measurement.getUbound());
				detail.setLbound(measurement.getLbound());
				detail.setSpcStandardValue(measurement.getSpcStandardValue());
				
				details.add(detail);
			}
		}
		
		this.add(details);
	}
	
}
